package com.njupt.HashTable;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 1. 两数之和
 */
public class TwoSum {

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int num = target - nums[i];
            if(map.containsKey(num)){
                return new int[] {i,map.get(num)};
            }else {
                map.put(nums[i],i);
            }
        }
        return new int[] {-1,-1};
    }


    public static void main(String[] args) {
        int[] nums = {3,2,4};
        TwoSum test = new TwoSum();
        int[] ints = test.twoSum(nums, 6);
        System.out.println(Arrays.toString(ints));
    }
}
